package leedcode_path.回溯;

import java.util.ArrayList;
import java.util.List;

public class 组合总和2 {
    /*
    * 输入：candidates = [2,3,6,7], target = 7
      输出：[[2,2,3],[7]]*/

    List<Integer> path = new ArrayList<Integer>();
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    int sum=0;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0);
        return res;
    }
    public void backtracking(int[] candidates,int target,int startindex){
        if (sum>target){
            return;
        }
        if (sum==target){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startindex; i <candidates.length ; i++) {
            sum+=candidates[i];
            path.add(candidates[i]);
            backtracking(candidates,target,i);
            sum-=candidates[i];
            path.remove(path.size()-1);
        }
    }
}
